package com.midian.service.permission.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.midian.common.dao.BaseDaoTemplet;
import com.midian.common.dao.OrderHelper;
import com.midian.common.dao.SearchHelper;
import com.midian.common.dao.SearchHelper.Type;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.model.permission.Auth;
import com.midian.service.common.BaseService;
import com.midian.service.permission.IAuthService;

/**
 * 权限业务实现类
 * @author Mengfh
 *
 */
@Service("authService")
public class AuthServiceImpl extends BaseService implements IAuthService {
	/**权限DAO*/
	private BaseDaoTemplet<Auth> authDAO;

	@Resource(name = "authDAO")
	public void setAuthDAO(BaseDaoTemplet<Auth> authDAO) {
		this.authDAO = authDAO;
	}

	@Override
	public Auth add(Auth target) throws BizException {
		this.authDAO.add(target);
		return target;
	}

	@Override
	public void update(Auth target) throws BizException {
		this.authDAO.update(target);
	}

	@Override
	public void delete(Serializable id) throws BizException {
		this.authDAO.delete(id);
	}

	@Override
	public void delete(Serializable[] ids) throws BizException {
		this.authDAO.delete(ids);
	}

	@Override
	public Auth get(Serializable id) throws BizException {
		return this.authDAO.get(id);
	}

	@Override
	public List<Auth> showList(Auth target, Pager pager,
			List<OrderHelper> helper) throws BizException {
		return this.authDAO.find(target, getSearchHelper(), helper, pager);
	}
	
	@Override
	protected List<SearchHelper> getSearchHelper() {
		String[] attrs = new String[]{"createTime", "keyName", "funcName", "isOp", "delFlag"};
		Type[] types = new Type[]{Type.BETWEEN, Type.ENDLIKE, Type.ENDLIKE, Type.EQ, Type.EQ};
		return SearchHelper.getSearchList(attrs, types);
	}
	
	/**
	 * 检测是否已存在权限
	 * @param keyName 权限key名称
	 * @return
	 */
	@Override
	public boolean isExistAuth(String keyName) {
		String hql = "select count(o) from Auth o where o.keyName = ?";
		return ((Number)this.authDAO.findObject(hql, new Object[]{keyName})).intValue() > 0;
	}

	@Override
	public void opDelFlag(Serializable[] ids, Integer status) throws BizException {
		this.authDAO.opDelFlag(ids, status);
	}
}
